<?php

namespace App\Console\Commands;
use App\Models\User\User;
use Illuminate\Console\Command;
use App\Models\Agent\TransferLogs;
use App\Models\Agent\ChaoZongdai;
use App\Models\Record\Record;
use App\Models\Log\ChaoMoneyLog;
use Illuminate\Support\Facades\DB;
class Transfer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:zongdai-transfer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $rel = TransferLogs::where('is_set', 0)->where("create_time", "<=", date('Y-m-d H:i:s',time()-120))->get();
        foreach($rel as $val) {
            DB::beginTransaction();
            $status = '转账成功';
            $scode = 1;
            $chaoz = ChaoZongdai::find($val->chao_id);
            $chao_money = $chaoz->money/100;
            $chao_money2 = $chaoz->money2/100;
            $trans_money = $val->trans_money/100;
            if($val->zongdai_id == 0) {
//                if($trans_money > 0 && $trans_money > $chao_money2) {
//                    $$status = '保险箱余额不足';
//                    $scode = 2;
//                }
//                if($trans_money < 0 && -1*$trans_money > $chao_money) {
//                    $$status = '余额不足';
//                    $scode = 3;
//                }
//                $transfer = TransferLogs::find($val->id);
//                $transfer->is_set = $scode;
//                $transfer->desc = $chaoz->name.'保险箱转账:'.$status;
//                $transfer->save();
//                if($scode == 1) {
//                    $chaoz->money += $trans_money * 100;
//                    $chaoz->money2 -= $trans_money * 100;
//                    $chaoz->save();
//
//                    $chaoMoneyLog = new ChaoMoneyLog();
//                    $chaoMoneyLog->chao_zongdai = $chaoz->id;
//                    $chaoMoneyLog->before = $chao_money;
//                    $chaoMoneyLog->after  = $chaoz->money;
//                    $chaoMoneyLog->money  = $chaoMoneyLog->after - $chaoMoneyLog->before;
//                    $chaoMoneyLog->detail = $chaoz->name . " 保险箱转账";
//                    $chaoMoneyLog->create_time = date("Y-m-d H:i:s");
//                    $chaoMoneyLog->save();
//                }
            } else {
                $zongdai = User::find($val->zongdai_id);
                $zongdai_money = $zongdai->money/100;

                if ($trans_money> 0 && $trans_money > $chao_money2) {
                    $status = '保险箱余额不足';
                    $scode = 2;
                } elseif ($trans_money < 0 && $trans_money < -$zongdai_money) {
                    $status = '伙伴余额不足';
                    $scode = 3;
                }
                $transfer = TransferLogs::find($val->id);
                $transfer->is_set = $scode;
                $transfer->desc = '['.$chaoz->name.']转账['.$zongdai->nickname.']:'.$status;
                $transfer->save();

                if($scode == 1) {
                    $chaoZOld = $chaoz->money2;
                    $chaoz->money2 -= $trans_money * 100;
                    $chaoz->save();

                    $zongOld = $zongdai->money;
                    $zongdai->money += $trans_money * 100;
                    $zongdai->save();

                    $chaoMoneyLog = new ChaoMoneyLog();
                    $chaoMoneyLog->chao_zongdai = $chaoz->id;
                    $chaoMoneyLog->before = $chaoZOld;
                    $chaoMoneyLog->after  = $chaoz->money2;
                    $chaoMoneyLog->money  = $chaoMoneyLog->after - $chaoMoneyLog->before;
                    $chaoMoneyLog->detail = "向伙伴转账 " . $zongdai->id;
                    $chaoMoneyLog->create_time = $val->create_time;
                    $chaoMoneyLog->save();

                    $zongdaiLog = new Record();
                    $zongdaiLog->user_id     = $zongdai->id;
                    $zongdaiLog->detail      = $chaoz->name . " 向伙伴转账";
                    $zongdaiLog->before      = $zongOld;
                    $zongdaiLog->after       = $zongdai->money;
                    $zongdaiLog->money       = $zongdaiLog->after - $zongdaiLog->before;
                    $zongdaiLog->type        = 8;
                    $zongdaiLog->create_time = $val->create_time;
                    $zongdaiLog->save();
                }
            }
            
            
            DB::commit();
        }
    }
}
